package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogAlbumBo;
import com.an.blog.admin.domain.vo.BlogAlbumVo;
import com.an.blog.service.IBlogAlbumService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;


/**
 * 博客相册控制器
 *
 * @author Ann
 * @date 2024/06/05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/album")
public class BlogAlbumController extends BaseController {

    private final IBlogAlbumService blogAlbumService;


    /**
     * 列表
     *
     * @param bo        薄
     * @param pageQuery 页面查询
     * @return {@link TableDataInfo }<{@link BlogAlbumVo }>
     * @author: Ann
     * @createDate: 2023/09/22 22:53:16
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:album:list")
    @GetMapping("/list")
    public TableDataInfo<BlogAlbumVo> list(BlogAlbumBo bo, PageQuery pageQuery) {
        return blogAlbumService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询博客相册列表
     *
     * @return {@link R }<{@link List }<{@link BlogAlbumVo }>>
     * @author: Ann
     * @createDate: 2023/05/10 21:53:52
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:album:list")
    @GetMapping("/queryAllList")
    public R<List<BlogAlbumVo>> queryAllList() {
        return R.ok(blogAlbumService.queryList(new BlogAlbumBo()));
    }

    /**
     * 获取博客相册详细信息
     *
     * @param albumId 主键
     */
    @SaCheckPermission("blog:album:query")
    @GetMapping("/{albumId}")
    public R<BlogAlbumVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long albumId) {
        return R.ok(blogAlbumService.queryById(albumId));
    }

    /**
     * 新增博客相册
     */
    @SaCheckPermission("blog:album:add")
    @Log(title = "相册管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogAlbumBo bo) {
        return toAjax(blogAlbumService.insertByBo(bo));
    }

    /**
     * 修改博客相册
     */
    @SaCheckPermission("blog:album:edit")
    @Log(title = "相册管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogAlbumBo bo) {
        return toAjax(blogAlbumService.updateByBo(bo));
    }

    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:49:08
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:album:edit")
    @Log(title = "相册管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogAlbumBo bo) {
        return toAjax(blogAlbumService.updateAlbumStatus(bo));
    }

    /**
     * 删除博客相册
     *
     * @param albumIds 主键串
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/11 11:14:32
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:album:remove")
    @Log(title = "相册管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{albumIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] albumIds) {
        return toAjax(blogAlbumService.deleteWithValidByIds(Arrays.asList(albumIds), true));
    }
}
